package com.example.config;
import com.example.entity.vo.RoleRight;
import com.example.service.ISysUserService;
import com.example.ulit.UsrIdByToken;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        // 设置响应类型和编码
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        // 记录异常信息，但不要在生产环境中暴露敏感信息
        // logger.error("Error checking user permissions", e);
        PrintWriter writer = response.getWriter();
        writer.print("{\"code\":401, \"message\":\"未登录\"}");
        writer.flush();
        writer.close();
    }
}